package br.com.conceito.dao;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

import br.com.conceito.util.DataUtil;
import br.com.conveito.entidade.Arquivo;

public class ArvoreDAO {

	private ArquivoDAO arquivoDAO = new ArquivoDAO();
	
	public TreeNode montaArvore() {
		TreeNode root = new DefaultTreeNode("Root",null);
		try {
			StringBuffer sql = new StringBuffer();
			sql.append("select * from arvore where id_pai is null");
			Connection connection = FabricaDAO.getInstancia().getConection();
			PreparedStatement statement = connection.prepareStatement(sql
					.toString());
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				TreeNode no = new DefaultTreeNode(rs.getString("nome"),root);
				montaProfundidade(rs.getInt("id"), no);
				
				List<Arquivo> arquivos = this.arquivoDAO.carregarArquivosDeUmNo(rs.getInt("id"));
				for(Arquivo arquivo : arquivos){
					new DefaultTreeNode(arquivo.getNome() +" - "+DataUtil.getInstancia().formataData(arquivo.getData()),no);
				}
			}
			
			

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return root;
	}

	private void montaProfundidade(final int index, TreeNode treeNode) {
		try {
			StringBuilder sql = new StringBuilder();
			sql.append("select id,id_pai,nome from arvore where id_pai =  ");
			sql.append(index);
			Connection connection = FabricaDAO.getInstancia().getConection();
			PreparedStatement statement = connection.prepareStatement(sql
					.toString());
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				StringBuffer buffer = new StringBuffer();
				buffer.append("<b><a id= ");
				buffer.append("\"");
				buffer.append(rs.getInt("id"));
				buffer.append("\"");
				buffer.append(" onclick= ");
				buffer.append("\"");
				buffer.append("load(this.id);");
				buffer.append("\"");
				buffer.append(">");
				buffer.append(rs.getString("nome"));
				buffer.append("</a></b>");
				buffer.append("");
				buffer.append("");
				TreeNode no = new DefaultTreeNode(buffer.toString(),treeNode);
				
				if(validaNoFilho(rs.getInt("id"))){
					montaProfundidade(rs.getInt("id"), no);
				}
				
				List<Arquivo> arquivos = this.arquivoDAO.carregarArquivosDeUmNo(rs.getInt(1));
				for(Arquivo arquivo : arquivos){
					new DefaultTreeNode(arquivo.getNome() +" - "+DataUtil.getInstancia().formataData(arquivo.getData()),no);
				}

				
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public boolean validaNoFilho(final int no){
		try {
			StringBuffer sql = new StringBuffer();
			sql.append("select * from arvore where id_pai =  ");
			sql.append(no);
			Connection connection = FabricaDAO.getInstancia().getConection();
			PreparedStatement statement = connection.prepareStatement(sql
					.toString());
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				return true;
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public List<Integer> obtemTodosNos(){
		List<Integer> ids = new ArrayList<Integer>();
		try {
			StringBuffer sql = new StringBuffer();
			sql.append("select id from arvore ");
			Connection connection = FabricaDAO.getInstancia().getConection();
			PreparedStatement statement = connection.prepareStatement(sql
					.toString());
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				ids.add(rs.getInt("id"));
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ids;
	}
	
	//TODO REMOVER METODO
	public static void main(String[] args) {
		String caminho = "c://x/c";
		File file = new File(caminho);
		System.out.println(file.isDirectory());
		file.mkdirs();
		System.out.println(file.isDirectory());
	}

}
